package com.example.f8399.testdemo.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.f8399.testdemo.BaseActivity;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.databinding.ActivityAvatorBinding;
import com.example.f8399.testdemo.utils.GlideImageLoader;
import com.isseiaoki.simplecropview.FreeCropImageView;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;
import com.theartofdev.edmodo.cropper.CropImage;

import java.io.File;
import java.util.ArrayList;

/**
 * 通过框架打开摄像头并裁剪
 * https://github.com/CysionLiu/ImagePicker
 */
public class AvatorActivity extends BaseActivity {

    ActivityAvatorBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_avator);

        initPicker();


    }

    /**
     * 通常这个都是在application中进行得
     */
    private void initPicker() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setMultiMode(false);
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setCrop(true);        //允许裁剪（单选才有效）
        imagePicker.setFreeCrop(true, FreeCropImageView.CropMode.FREE);//新版添加,自由裁剪，优先于setCrop
        imagePicker.setSaveRectangle(false); //是否按矩形区域保存
        imagePicker.setSelectLimit(1);    //选中数量限制
        imagePicker.setStyle(CropImageView.Style.CIRCLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);   //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);  //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);//保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);//保存文件的高度。单位像素
    }

    private static final int IMAGE_PICKER = 0xa3;
//    private static final int REQUEST_CODE_SELECT = 0xa4;

    /**
     * 国内的库：https://github.com/CysionLiu/ImagePicker
     * @param view
     */
    public void takePic(View view) {
        Intent intent = new Intent(this, ImageGridActivity.class);
        startActivityForResult(intent, IMAGE_PICKER);
    }

    public void openGouNei(View view) {
        //若拍照之后需要裁剪，则在使用前，需要关闭多选模式，支持裁剪

        ImagePicker.getInstance().setMultiMode(false);
        ImagePicker.getInstance().setFreeCrop(true, FreeCropImageView.CropMode.FREE);

        Intent intent = new Intent(this, ImageGridActivity.class);
        intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS,true); // 是否是直接打开相机
        startActivityForResult(intent, IMAGE_PICKER);
    }

    /**
     * 添加国外的库：https://github.com/ArthurHub/Android-Image-Cropper
     * @param view
     */
    public void openCamera(View view) {
        //设置参考：https://github.com/ArthurHub/Android-Image-Cropper/wiki/Visual-Customization
        CropImage.activity()
                .setCropShape(com.theartofdev.edmodo.cropper.CropImageView.CropShape.OVAL)//设置剪切框为椭圆
                .start(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //国内
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == IMAGE_PICKER) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                ImageItem imageItem = images.get(0);
                File file = new File(imageItem.path);
                Glide.with(AvatorActivity.this).load(file).into(mBinding.img);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }

        //国外
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                Uri resultUri = result.getUri();
                Glide.with(AvatorActivity.this).load(resultUri).into(mBinding.img);

            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
            }
        }
    }

}
